//
//  ZZFLoginV.m
//  ZZFUCComponent
//
//  Created by quheng on 2017/8/29.
//  Copyright © 2017年 quheng. All rights reserved.
//

#import "BZUCLoginV.h"
#import <Masonry/Masonry.h>
#import <BZPreHeaderComponent/BZPreHeaderComponent.h>
#import "UIView+ViewController.h"
#import "BZUCRegisterVC.h"
#import "BZUCForgetVC.h"
#import "BZUCLoginService.h"
#import "NSString+NormalRegex.h"
#import <MBProgressHUD/MBProgressHUD.h>
#import "BZUCCurrentUser.h"
#import <Mantle/Mantle.h>
#import "BZUCComponentHeader.h"
#import <BZBaseWidget/BZBaseWidget.h>
#import <BZDataBaseSDK/BZDataBaseManager.h>
#import <Mantle/Mantle.h>
#import "BZBundleKit.h"


@interface BZUCLoginV()

@end

@implementation BZUCLoginV

- (instancetype)init{

    self = [super init];
    if (self) {
        
        [self configureV];
    }

    return self;
}

- (void)configureV{

    
    UIImageView *buddhaIconV = [[UIImageView alloc]init];
    buddhaIconV.image = [BZBundleResourcesKit imageWithName:kLoginHomeImageName
                                                       type:kImageType
                                                 bundleName:kBundleName
                                                inDirectory:kBundleDirectory];
    [self addSubview:buddhaIconV];

    [buddhaIconV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self).with.offset(72);
        make.size.mas_offset(CGSizeMake(90, 125));
    }];
    
    BZUCInputV *accountInputV = [[BZUCInputV alloc]init];
    accountInputV.index = 0;
    [self addSubview:accountInputV];
    self.accountV = accountInputV;
    
    
    [accountInputV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).with.offset(48);
        make.right.equalTo(self).with.offset(-48);
        make.height.mas_offset(32);
        make.top.equalTo(buddhaIconV.mas_bottom).with.offset(74);
    }];
    
    
    BZUCInputV *pswInputV = [[BZUCInputV alloc]init];
    pswInputV.index = 1;
    [self addSubview:pswInputV];
    self.pswV = pswInputV;

    [pswInputV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).with.offset(48);
        make.right.equalTo(self).with.offset(-48);
        make.height.mas_offset(32);
        make.top.equalTo(accountInputV.mas_bottom).with.offset(12);
    }];
    
    
    UIButton *registerBtn = [[UIButton alloc]init];
    [registerBtn addTarget:self action:@selector(clickRegisterBtn:) forControlEvents:UIControlEventTouchUpInside];
    registerBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [registerBtn setTitleColor:[UIColor colorWithRed:0.63 green:0.82 blue:0.61 alpha:1.00] forState:UIControlStateNormal];
    [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    [self addSubview:registerBtn];
    
    
    [registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(pswInputV.mas_left);
        make.top.equalTo(pswInputV.mas_bottom).with.offset(12);
        make.size.mas_offset(CGSizeMake(30, 15));
        
    }];
    
    UIButton *forgetBtn = [[UIButton alloc]init];
    [forgetBtn addTarget:self action:@selector(clickForgetBtn:) forControlEvents:UIControlEventTouchUpInside];
    forgetBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [forgetBtn setTitleColor:[UIColor colorWithRed:0.94 green:0.38 blue:0.31 alpha:1.00] forState:UIControlStateNormal];
    [forgetBtn setTitle:@"忘记密码" forState:UIControlStateNormal];
    [self addSubview:forgetBtn];
    
    [forgetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(pswInputV.mas_right);
        make.top.equalTo(pswInputV.mas_bottom).with.offset(12);
        make.size.mas_offset(CGSizeMake(60, 15));
    }];

    UIButton *loginBtn = [[UIButton alloc]init];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    loginBtn.backgroundColor = [UIColor colorWithRed:1.00 green:0.42 blue:0.12 alpha:1.00];
    loginBtn.layer.cornerRadius = 8;
    loginBtn.layer.masksToBounds = YES;
    [loginBtn addTarget:self action:@selector(clickLoginBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:loginBtn];
    self.loginBtn = loginBtn;
    
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(forgetBtn.mas_bottom).with.offset(40);
        make.left.equalTo(self).with.offset(58);
        make.right.equalTo(self).with.offset(-58);
        make.height.mas_offset(36);
    }];
    
    accountInputV.textF.text = [BZUCCurrentUser shareInstance].userModel.phone;
    pswInputV.textF.text     = [BZUCCurrentUser shareInstance].userModel.password;
    
}


- (void)clickRegisterBtn:(UIButton *)sender{

    UINavigationController *nav =  [[self viewController]navigationController];
    BZUCRegisterVC *registerVC = [[BZUCRegisterVC alloc]init];
    [nav pushViewController:registerVC animated:YES];
    
}

- (void)clickForgetBtn:(UIButton *)sender{
    
    UINavigationController *nav =  [[self viewController]navigationController];
    BZUCForgetVC *forgetVC       = [[BZUCForgetVC alloc]init];
    forgetVC.accountText        = self.accountV.textF.text;
    forgetVC.accountBlock = ^(NSString *text) {
        if ([text length]) {
            [BZUCCurrentUser shareInstance].userModel.phone = text;
        }
    };
    [nav pushViewController:forgetVC animated:YES];
    
    
    
    
    
}

- (void)clickLoginBtn:(UIButton *)sender{
    
    
    BZUCLoginService *loginService = [[BZUCLoginService alloc]init];
    
    
    if (![self.accountV.textF.text length]) {
        [MBProgressHUD showClueInView:self title:@"手机号未填写" duration:0.7];
        
        return;
    }
    
    if (![NSString isValidateMobile:self.accountV.textF.text]) {
        
        [MBProgressHUD showClueInView:self title:@"手机号输入有误" duration:0.7];
        
        return;
    }
    
    if (![self.pswV.textF.text length]) {
        [MBProgressHUD showClueInView:self title:@"密码未填写" duration:0.7];
        return;
    }
    
    if (![NSString isValidatePassword:self.pswV.textF.text]) {
        [MBProgressHUD showClueInView:self title:@"密码为6~20位字母、数字、点、下划线、减号组合" duration:0.7];
        return;
    }
    
    [MBProgressHUD showHUDAddedTo:self animated:YES];
    
    [self.pswV.textF resignFirstResponder];
    [self.accountV.textF resignFirstResponder];

    [loginService loginByUserName:self.accountV.textF.text
                         password:self.pswV.textF.text
                          success:^(NSString *code, id responseObject) {
                              
                              BZUCUserModel *userModel = [[BZUCUserModel alloc]init];
                              userModel = [MTLJSONAdapter modelOfClass:[BZUCUserModel class] fromJSONDictionary:responseObject error:nil];
                              userModel.password = self.pswV.textF.text;
                              [BZUCCurrentUser shareInstance].userModel = userModel;
                              [MBProgressHUD hideHUDForView:self];
                              [MBProgressHUD showSuccess:@"登录成功" toView:self];
                              self.accountV.textF.userInteractionEnabled = YES;

                              
                              if (self.delegate && [self.delegate respondsToSelector:@selector(didLoginSuccess:)]) {
                                  
                                  [self.delegate didLoginSuccess:self];
                              }
                              
        
    } failure:^(NSString *code, NSError *error) {
        
        self.accountV.textF.userInteractionEnabled = YES;
        [MBProgressHUD hideHUDForView:self];
//        [MBProgressHUD showError:@"登录失败" toView:self];
        
    }];
    
    
    
}








@end
